package cn.seqdata.javatime;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;

public enum RelativeTime {
	/**
	 * 最新
	 */
	PT0S(0, ChronoUnit.SECONDS, Duration.ZERO) {
		@Override
		public DateTimeInterval toInterval(LocalDateTime occur) {
			return new DateTimeInterval(occur, occur);
		}
	},
	/**
	 * 近1秒
	 */
	PT1S(1, ChronoUnit.SECONDS, Duration.ofSeconds(1)),
	/**
	 * 近3秒
	 */
	PT3S(3, ChronoUnit.SECONDS, Duration.ofSeconds(3)),
	/**
	 * 近5秒
	 */
	PT5S(5, ChronoUnit.SECONDS, Duration.ofSeconds(5)),
	/**
	 * 近15秒
	 */
	PT15S(15, ChronoUnit.SECONDS, Duration.ofSeconds(15)),
	/**
	 * 近30秒
	 */
	PT30S(30, ChronoUnit.SECONDS, Duration.ofSeconds(30)),
	/**
	 * 近1分钟
	 */
	PT1M(1, ChronoUnit.MINUTES, Duration.ofMinutes(1)),
	/**
	 * 近5分钟
	 */
	PT5M(5, ChronoUnit.MINUTES, Duration.ofMinutes(5)),
	/**
	 * 近15分钟
	 */
	PT15M(15, ChronoUnit.MINUTES, Duration.ofMinutes(15)),
	/**
	 * 近30分钟
	 */
	PT30M(30, ChronoUnit.MINUTES, Duration.ofMinutes(30)),
	/**
	 * 近1小时
	 */
	PT1H(1, ChronoUnit.HOURS, Duration.ofHours(1)),
	/**
	 * 近2小时
	 */
	PT2H(2, ChronoUnit.HOURS, Duration.ofHours(2)),
	/**
	 * 近3小时
	 */
	PT3H(3, ChronoUnit.HOURS, Duration.ofHours(3)),
	/**
	 * 近4小时
	 */
	PT4H(4, ChronoUnit.HOURS, Duration.ofHours(4)),
	/**
	 * 近6小时
	 */
	PT6H(6, ChronoUnit.HOURS, Duration.ofHours(6)),
	/**
	 * 近8小时
	 */
	PT8H(8, ChronoUnit.HOURS, Duration.ofHours(8)),
	/**
	 * 近12小时
	 */
	PT12H(12, ChronoUnit.HOURS, Duration.ofHours(12)),
	/**
	 * 近24小时
	 */
	PT24H(1, ChronoUnit.DAYS, Duration.ofHours(24), Period.ofDays(1)),
	/**
	 * 近1天
	 */
	P1D(1, ChronoUnit.DAYS, Duration.ofDays(1), Period.ofDays(1)),
	/**
	 * 近3天
	 */
	P3D(3, ChronoUnit.DAYS, Duration.ofDays(3), Period.ofDays(3)),
	/**
	 * 近1周
	 */
	P1W(7, ChronoUnit.DAYS, Duration.ofDays(7), Period.ofWeeks(1)),
	/**
	 * 近2周
	 */
	P2W(14, ChronoUnit.DAYS, Duration.ofDays(14), Period.ofWeeks(2)),
	/**
	 * 近1个月
	 */
	P1M(1, ChronoUnit.MONTHS, Period.ofMonths(1)),
	/**
	 * 近2个月
	 */
	P2M(2, ChronoUnit.MONTHS, Period.ofMonths(2)),
	/**
	 * 近3个月
	 */
	P3M(3, ChronoUnit.MONTHS, Period.ofMonths(3)),
	/**
	 * 近6月
	 */
	P6M(6, ChronoUnit.MONTHS, Period.ofMonths(6)),
	/**
	 * 近1年
	 */
	P1Y(1, ChronoUnit.YEARS, Period.ofYears(1)),
	/**
	 * 近2年
	 */
	P2Y(2, ChronoUnit.YEARS, Period.ofYears(2)),
	/**
	 * 近5年
	 */
	P5Y(5, ChronoUnit.YEARS, Period.ofYears(5)),
	/**
	 * 近10年
	 */
	P10Y(1, ChronoUnit.DECADES, Period.ofYears(10));

	public final int value;
	public final ChronoUnit unit;
	public final Duration duration;
	public final Period period;

	RelativeTime(int value, ChronoUnit unit, Duration duration, Period period) {
		this.value = value;
		this.unit = unit;
		this.duration = duration;
		this.period = period;
	}

	RelativeTime(int value, ChronoUnit unit, Duration duration) {
		this(value, unit, duration, null);
	}

	RelativeTime(int value, ChronoUnit unit, Period period) {
		this(value, unit, null, period);
	}

	public DateTimeInterval toInterval() {
		return toInterval(LocalDateTime.now());
	}

	public DateTimeInterval toInterval(LocalDateTime occur) {
		return new DateTimeInterval(period, occur);
	}

	public DateTimeInterval toInterval(LocalDate date) {
		//截至时间为24:00
		return new DateTimeInterval(period, JavaTimeUtils.toLocalDateTime(date.plusDays(1)));
	}
}
